import {Link} from 'react-router-dom'
import {useState} from 'react'
import axios from 'axios'
import withRouter from '@/hoc/withRouter'
const Login=(props)=>{
    console.log('props',props);
    const [user,setUser]=useState({
        username:'mrbird',
        password:'1234qwer'
    })
    const login=(e)=>{
        console.log('user',user);
        axios({
            method:'POST',
            url:'http://xawn.x3322.net:8012/login',
            data:user,
            headers:{
                'Content-Type':'application/x-www-form-urlencoded'
            }
        }).then(res=>{
            console.log('res',res.data);
            if(res.data.message==="认证成功"){
                localStorage.setItem('token',res.data.data.token)
                localStorage.setItem('user',JSON.stringify(res.data.data.user))
                //路由跳转
                props.nav("/")
            }
        }).catch(err=>{
            alert('用户名或者密码有误，请重写登录')
        })
        e.preventDefault()
    }
    return(<>
        
        <h1>登录页面</h1>
        <div>
            <form>
                <div>
                    <label>用户名:</label>
                    <input type="text" placeholder="请输入用户名" value={user.username} onChange={e=>setUser({...user,username:e.target.value})}></input>
                </div>
                <div>
                    <label>密码:</label>
                    <input type="password" placeholder="请输入密码" value={user.password} onChange={e=>setUser({...user,password:e.target.value})}></input>
                </div>
                <div>
                    <button onClick={login}>登录</button>
                    没有账号，请先<Link to={"/register"}>注册</Link>
                </div>
            </form>
        </div>
    </>)
}
export default withRouter(Login)